package os;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Paulius Dambrauskas
 */
public class StartStop extends Process{

    @Override
    public void runProcess() {
        //Sisteminiai Procesai
        this.changeStepCell(step);
        switch(step){
            case 1:{
                //changeStepCell(1);
                OS.createRes(selfId, "Pertraukimo");
                OS.createRes(selfId, "Pertraukimo pranešimo");
                OS.createRes(selfId, "OS Pabaiga");
                OS.createRes(selfId, "Užduoties duomenys");
                OS.createRes(selfId, "Įvestis");
                OS.createRes(selfId, "Užduotis");
                OS.createRes(selfId, "ILaukimas");
                OS.createRes(selfId, "Pakrovimo paketas");
                OS.createRes(selfId, "Paleidimas iš VM");
                OS.createRes(selfId, "Eilutė");
                OS.createRes(selfId, "3 Kanalas"); 
                OS.createRes(selfId, "2 Kanalas");  
                OS.createRes(selfId, "1 Kanalas");  
                OS.createRes(selfId, "Loader darbo pabaiga"); 
                OS.createRes(selfId, "Vartotojo atm."); 
                OS.createRes(selfId, "Vartotojo įvestis");
                OS.createRes(selfId, "Eilutė");
                OS.createRes(selfId, "ILaukimas");
                OS.createRes(selfId, "Supervizorinė atmintis");
                OS.createRes(selfId, "Lempa"); 
                OS.createRes(selfId, "4 Kanalas");
                //kanalai iskart buna
                OS.createResElem(new ResourceElement("4 Kanalas", true)); 
                OS.createResElem(new ResourceElement("3 Kanalas", true)); 
                OS.createResElem(new ResourceElement("2 Kanalas", true));
                OS.createResElem(new ResourceElement("1 Kanalas", true));
                //3 vartotojo programos = 3vnt
                OS.createResElem(new ResourceElement("Vartotojo atm.", true));
                OS.createResElem(new ResourceElement("Vartotojo atm.", true));
                OS.createResElem(new ResourceElement("Vartotojo atm.", true));
                //Sp reursas
                OS.createResElem(new ResourceElement("Supervizorinė atmintis", true));
                OS.createResElem(new ResourceElement("Supervizorinė atmintis", true));
                OS.createResElem(new ResourceElement("Supervizorinė atmintis", true));
                OS.window.inputBtn.setDisable(false);
                incStep(1);
                break;
            }
            case 2:{
                //changeStepCell(2);
                OS.createProc(selfId, 2, 99, MainProc.class);
                OS.createProc(selfId, 2, 98, Read.class);
                OS.createProc(selfId, 2, 97, JCL.class);
                OS.createProc(selfId, 2, 96, Input.class);
                OS.createProc(selfId, 2, 95, Loader.class);
                OS.createProc(selfId, 2, 94, PrintLine.class);
                OS.createProc(selfId, 2, 93, Interrupt.class);
                OS.createProc(selfId, 2, 92, StartFromVm.class);
                OS.createProc(selfId, 2, 91, Indicator.class);
                incStep(1);
                break;
            }
            case 3:{
                //changeStepCell(3);
                OS.askRes("OS Pabaiga", selfId);
                incStep(1);
                break;
            }
            case 4:{
                //changeStepCell(4);
                //nAIKINAM Procesus
                ArrayList<Integer> destroy = new ArrayList<>();
                for(int i : this.children){
                    destroy.add(i);
                }
                for(int i = 0; i < destroy.size(); i++)
                    OS.destroyProc(destroy.get(i));
                incStep(1);
                break;
            }
            case 5:{
                //changeStepCell(5);
                //naikinam resursus
                ArrayList<String> destroy = new ArrayList<>();
                for(String s : this.createdResources){
                    destroy.add(s);
                }
                for(int i = 0; i < destroy.size(); i++)
                    try{
                        OS.destroyRes(destroy.get(i));
                    } catch (Exception e){}
                incStep(1);
                break;
            }
            case 6:{
                //changeStepCell(6);
                //Nusižudyt
                OS.window.step = false;
                
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(StartStop.class.getName()).log(Level.SEVERE, null, ex);
                }
                System.exit(0);
                break;
            }
                    
        }
       
    }
    


}
